package com.help10000.rms.ui.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amir.common.api.ApiContents
import com.amir.common.beans.CustomKeyValueItemInfo
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseRecyclerViewAdapter
import com.amir.common.utils.DisplayUtil
import com.amir.common.utils.IMEUtil
import com.amir.common.utils.LogUtils
import com.amir.common.utils.PhoneUtils
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.common.GridSpacingItemDecoration
import com.help10000.rms.mvp.view.ICustomKeyValueItemView
import com.help10000.rms.ui.activitys.MapNavigationActivity
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory
import com.tencent.tencentmap.mapsdk.maps.MapView
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition
import com.tencent.tencentmap.mapsdk.maps.model.LatLng
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions

class OrderChildItemAdapter(
    context: Context?,
    dataList: List<CustomKeyValueItemInfo>?,
    player: ICustomKeyValueItemView
) :
    BaseRecyclerViewAdapter<CustomKeyValueItemInfo>(context, dataList) {
    private var player: ICustomKeyValueItemView = player
    val VIEW_TYPE_TEXT = 1
    val VIEW_TYPE_TEL = 2
    val VIEW_TYPE_ADDR = 3
    val VIEW_TYPE_MAP = 4
    val VIEW_TYPE_PHOTO = 5
    val VIEW_TYPE_VOICE = 6

    override fun onViewDetachedFromWindow(holder: CommonViewHolder) {
        super.onViewDetachedFromWindow(holder)
        var pos = holder.adapterPosition
        var viewType = getItemViewType(pos)
        if (viewType == VIEW_TYPE_MAP) {
            var map = holder.getView<MapView>(R.id.map)
            map?.onDestroy()
            LogUtils.E("地图销毁")
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        var item: View? = null
        when (viewType) {
            VIEW_TYPE_TEXT -> {
                item = layoutInflater.inflate(R.layout.item_order_text_item, parent, false)
            }
            VIEW_TYPE_TEL -> {
                item = layoutInflater.inflate(R.layout.item_order_text_item, parent, false)
            }
            VIEW_TYPE_ADDR -> {
                item = layoutInflater.inflate(R.layout.item_order_text_item, parent, false)
            }
            VIEW_TYPE_MAP -> {
                item = layoutInflater.inflate(R.layout.item_order_text_map, parent, false)
            }
            VIEW_TYPE_PHOTO -> {
                item = layoutInflater.inflate(R.layout.item_order_item_photo, parent, false)
            }
            VIEW_TYPE_VOICE -> {
                item = layoutInflater.inflate(R.layout.item_order_text_item, parent, false)
            }
            else -> {
                item = layoutInflater.inflate(R.layout.item_order_text_item, parent, false)
            }

        }
        return CommonViewHolder(item)
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        var data = dataList.get(position)
        var viewType = getItemViewType(position)
        when (viewType) {
            VIEW_TYPE_TEXT -> {
                var title = holder.getView<TextView>(R.id.orderItemTitleTv)
                var detail = holder.getView<TextView>(R.id.orderItemDetailTv)
                if (data.Name.isNullOrEmpty())
                    title.visibility = View.GONE
                else
                    title.setText(data.Name)
                detail.setText(data.Val)
                if (!data.ValColor.isNullOrEmpty()) {
                    detail.setTextColor(Color.parseColor(data.ValColor))
                }
                detail.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
            }
            VIEW_TYPE_TEL -> {
                var title = holder.getView<TextView>(R.id.orderItemTitleTv)
                var detail =
                    holder.getView<TextView>(R.id.orderItemDetailTv)
                if (data.Name.isNullOrEmpty())
                    title.visibility = View.GONE
                else
                    title.setText(data.Name)
                detail.setText(data.Val)
                if (!data.ValColor.isNullOrEmpty()) {
                    detail.setTextColor(Color.parseColor(data.ValColor))
                }
                detail.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    null,
                    mContext.resources.getDrawable(R.mipmap.dh),
                    null
                )
                detail.setOnClickListener(DuplicateClickProxy {
                    PhoneUtils.callPhone(mContext, data.Val)
                })
            }
            VIEW_TYPE_ADDR -> {
                var title = holder.getView<TextView>(R.id.orderItemTitleTv)
                var detail =
                    holder.getView<TextView>(R.id.orderItemDetailTv)
                if (data.Name.isNullOrEmpty())
                    title.visibility = View.GONE
                else
                    title.text = data.Name
                detail.text = data.Val
                if (!data.ValColor.isNullOrEmpty()) {
                    detail.setTextColor(Color.parseColor(data.ValColor))
                }
                detail.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    null,
                    mContext.resources.getDrawable(R.mipmap.ddr),
                    null
                )
                holder.setOnClickListener(R.id.orderItemDetailTv) {
                    val intent = Intent(mContext, MapNavigationActivity::class.java)
                    intent.putExtra("data", data.Val)
                    mContext.startActivity(intent)
                }
            }
            VIEW_TYPE_MAP -> {
                var title = holder.getView<TextView>(R.id.orderItemTitleTv)
                if (data.Name.isNullOrEmpty())
                    title.visibility = View.GONE
                else
                    title.text = data.Name
                var mapView = holder.getView<MapView>(R.id.map)
                var mTencentMap = mapView.map
                var location = data.Val.split(',')
                var latLng = LatLng(location[0].toDouble(), location[1].toDouble())
                val latLngs = mutableListOf<LatLng>()
                latLngs.add(latLng)
                mTencentMap?.addMarker(
                    MarkerOptions(latLng)
                        .anchor(0.5f, 1f)
                )
                val cameraSigma = CameraUpdateFactory.newCameraPosition(
                    CameraPosition(
                        latLng, //中心点坐标，地图目标经纬度 //目标缩放级别
                        12f,
                        0f,
                        0f

                    )
                ) //目标旋转角 0~360° (正北方为0)
                mTencentMap?.moveCamera(cameraSigma) //移动地图
                mapView.onResume()


                mapView.map.setOnMapClickListener {
                    val intent = Intent(mContext, MapNavigationActivity::class.java)
                    intent.putExtra("location", data.Val)
                    mContext.startActivity(intent)
                }


            }
            VIEW_TYPE_PHOTO -> {
                var recyclerView = holder.getView<RecyclerView>(R.id.photosRecyclerView)
                recyclerView.apply {
                    itemAnimator = null
                    layoutManager = GridLayoutManager(context, 4)
                    var photos =
                        data.Val!!.replace("\"", "").replace("[", "").replace("]", "").split(',')
                    adapter = OrderPhotoItemAdapter(context, photos, R.layout.item_order_photo_item)
                    addItemDecoration(
                        GridSpacingItemDecoration(
                            DisplayUtil.dip2px(context, 5f),
                            true
                        )
                    )
                }
            }
            VIEW_TYPE_VOICE -> {
                var title = holder.getView<TextView>(R.id.orderItemTitleTv)
                var detail =
                    holder.getView<TextView>(R.id.orderItemDetailTv)
                if (data.Name.isNullOrEmpty())
                    title.visibility = View.GONE
                else
                    title.text = data.Name
                detail.text = null
                detail.setCompoundDrawablesWithIntrinsicBounds(
                    null,
                    null,
                    mContext.resources.getDrawable(R.mipmap.icon_voice),
                    null
                )
                holder.setOnClickListener(R.id.orderItemDetailTv) {
                    player.playVoice(ApiContents.ImageUrl + data.Val)
                }

            }
            else -> {

            }

        }
        holder.setCommonClickListener(object : CommonViewHolder.onItemCommonClickListener {
            override fun onItemClickListener(position: Int) {
                IMEUtil.hideIME(mContext, holder.itemView)
            }

            override fun onItemLongClickListener(position: Int) {

            }

        })
    }

    override fun getItemViewType(position: Int): Int {
        var data = dataList.get(position)
        when (data.Type) {
            "" -> {
                return VIEW_TYPE_TEXT
            }
            "tel" -> {
                return VIEW_TYPE_TEL
            }
            "addr" -> {
                return VIEW_TYPE_ADDR
            }
            "map" -> {
                return VIEW_TYPE_MAP
            }
            "photo" -> {
                return VIEW_TYPE_PHOTO
            }
            "movie" -> {
                return VIEW_TYPE_VOICE
            }
        }
        return super.getItemViewType(position)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }


}
